--[[
Michael Lindsey "SvK": 
-Version 1.1
-Not localized
]]--
STAT_EQUIPPED_ITEM_LEVEL = "Equipped Item Level"

tinsert(PAPERDOLL_STATCATEGORIES["GENERAL"].stats, #PAPERDOLL_STATCATEGORIES["GENERAL"].stats + 1, "EQUIPPEDITEMLEVEL"); 
				
PAPERDOLL_STATINFO ["EQUIPPEDITEMLEVEL"] = 
{
	updateFunc = 
		function(statFrame, unit) 
			PaperDollFrame_SetEquipItemLevel(statFrame, unit); 
		end
};
					
function PaperDollFrame_SetEquipItemLevel(statFrame, unit)
	local EIL = GetEquippedItemLevel();
	PaperDollFrame_SetLabelAndText(statFrame, STAT_EQUIPPED_ITEM_LEVEL, floor(EIL), false);
	statFrame.tooltip = HIGHLIGHT_FONT_COLOR_CODE..STAT_EQUIPPED_ITEM_LEVEL.." "..floor(EIL).." ("..EIL..")"..FONT_COLOR_CODE_CLOSE;
	statFrame.tooltip2 = "The average item level of your equipped items.  This number is not used to calculate if you can enter a heroic dungeon.";
end

function GetEquippedItemLevel()

	--All classes should have an item equipped in all of these slots at max level
	local AllClassesItemSlots = 
	{
		"HeadSlot", 		--1
		"NeckSlot", 		--2
		"ShoulderSlot", 	--3
		"BackSlot", 		--4
		"ChestSlot",		--5
		
	--	"ShirtSlot",		--x
	--	"TabardSlot",		--x
	
		"WristSlot",		--6
		"HandsSlot",		--7
		"WaistSlot",		--8
		"LegsSlot",			--9
		"FeetSlot",			--10

		"Finger0Slot",		--11
		"Finger1Slot",		--12
			
		"Trinket0Slot",		--13
		"Trinket1Slot",		--14
			
		"RangedSlot",		--15
		"MainHandSlot",		--16
	}
	local DualWeildItemSlots =
	{
		"SecondaryHandSlot",	--1
	}
	
	local iLevelCount = 0;
	--local i = 1;
	local imax = #AllClassesItemSlots
	
	for i=1,imax do
		 iLevelCount = iLevelCount + GetItemLevel(AllClassesItemSlots[i]);
		i = i + 1;
	end

	if GetDualWeildIndicator() == true then
		iLevelCount = iLevelCount + GetItemLevel(DualWeildItemSlots[1]);
		i = #AllClassesItemSlots + 1
	else
		i = #AllClassesItemSlots
	end
	--For Debugging
	--print(iLevelCount.." "..i.." "..floor((iLevelCount/i)).." "..(iLevelCount/i)..i);
	return (iLevelCount/i) 
end

function GetItemLevel(slot)
	if GetInventoryItemID("Player", GetInventorySlotInfo(slot)) ~= nil then
		local _, _, _, iLevel = GetItemInfo(GetInventoryItemID("Player", GetInventorySlotInfo(slot)));
		return iLevel;
	else 
		return 0;
	end
end

function GetMainHandSlotEquipSlot()
	if GetInventoryItemID("Player", GetInventorySlotInfo("MainHandSlot")) ~= nil then
		local _, _, _, _, _, _, _, _, equipSlot = GetItemInfo(GetInventoryItemID("Player", GetInventorySlotInfo("MainHandSlot")));
		return equipSlot;
	else 
		return nil;
	end
end

function GetMainHandSubClassType()
	if GetInventoryItemID("Player", GetInventorySlotInfo("MainHandSlot")) ~= nil then
		local _, _, _, _, _, _, subclass = GetItemInfo(GetInventoryItemID("Player", GetInventorySlotInfo("MainHandSlot")));
		return subclass;
	else 
		return nil;
	end
end

function GetDualWeildIndicator()
	local _, classFileName = UnitClass("player");
	local _, _, _, _, rank = GetTalentInfo(2, 20, false, false, nil);
	
	if classFileName == "WARRIOR" and 1 == rank then
		return true;
	else
		if GetMainHandSlotEquipSlot() == "INVTYPE_2HWEAPON" then
			return false;
		elseif GetMainHandSlotEquipSlot() == nil then
			return true;
		else
			return true;
		end
	end
end